package com.waypolice.manager.service.impl;

import com.waypolice.common.pojo.DestroyCommonResult;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.manager.service.ArcAppraisalService;
import com.waypolice.manager.service.ArcCreateService;
import com.waypolice.manager.service.ArcDestroyService;
import com.waypolice.mapper.*;
import com.waypolice.mapper.handwriting.CmsAppraisalMapperSon;
import com.waypolice.mapper.handwriting.CmsDestroyMapperSon;
import com.waypolice.mapper.handwriting.CmsJudItemMapperSon;
import com.waypolice.pojo.*;
import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 档案著录实现类
 * @author zry
 *
 */
@Service
public class ArcDestroyServiceImpl implements ArcDestroyService{

    @Autowired
    private CmsAppraisalMapper cmsAppraisalMapper;
    @Autowired
    private CmsDestroyMapper cmsDestroyMapper;
    @Autowired
    private CmsDestroyMapperSon cmsDestroyMapperSon;
    @Autowired
    private CmsDestroyArchiveMapper cmsDestroyArchiveMapper;

    @Override
    public List<CmsAppraisal> searchNeedDestroyVolumeMessage() {
        CmsAppraisalExample cmsAppraisalExample = new CmsAppraisalExample();
        CmsAppraisalExample.Criteria criteria = cmsAppraisalExample.createCriteria();
        criteria.andAppExtratwoEqualTo("0");
        criteria.andAppExtrafourEqualTo("同意");
        List<CmsAppraisal> list = new ArrayList<>();
        List<CmsAppraisal> cmsAppraisals = cmsAppraisalMapper.selectByExample(cmsAppraisalExample);
        for (CmsAppraisal cmsAppraisal:cmsAppraisals) {
            CmsDestroyArchiveExample cmsDestroyArchiveExample = new CmsDestroyArchiveExample();
            CmsDestroyArchiveExample.Criteria criteria1 = cmsDestroyArchiveExample.createCriteria();
            Long apprArcId = cmsAppraisal.getApprArcId();
            Integer apprType = cmsAppraisal.getApprType();
            criteria1.andDmArcIdEqualTo(apprArcId.intValue());
            criteria1.andDmArctypeEqualTo(apprType);
            List<CmsDestroyArchive> cmsDestroyArchives = cmsDestroyArchiveMapper.selectByExample(cmsDestroyArchiveExample);
            if(cmsDestroyArchives.isEmpty()){
                list.add(cmsAppraisal);
            }
        }
        return list;
    }
    @Override
    public TaotaoResult chooseCodeRepeat(String desCode) {
        TaotaoResult result = new TaotaoResult();
        CmsDestroyExample cmsDestroyExample = new CmsDestroyExample();
        CmsDestroyExample.Criteria criteria = cmsDestroyExample.createCriteria();
        criteria.andDesCodeEqualTo(desCode);
        List<CmsDestroy> cmsDestroys = cmsDestroyMapper.selectByExample(cmsDestroyExample);
        if(cmsDestroys.isEmpty()){
            result.setStatus(200);
        }else{
            result.setStatus(101);
        }
        return result;
    }

    @Override
    public List<CmsDestroy> searchHistoryDesMessage() {
        CmsDestroyExample cmsDestroyExample = new CmsDestroyExample();
        CmsDestroyExample.Criteria criteria = cmsDestroyExample.createCriteria();
        criteria.getAllCriteria();
        cmsDestroyExample.setOrderByClause("\"DES_CREATETIME\" DESC");
        List<CmsDestroy> cmsDestroys = cmsDestroyMapper.selectByExample(cmsDestroyExample);
        return cmsDestroys;
        }

    @Override
    public CmsDestroy  checkDestroyArcByDescode(String desId) {
        CmsDestroy cmsDestroy = cmsDestroyMapper.selectByPrimaryKey(Integer.valueOf(desId));
        return cmsDestroy;
    }

    @Override
    public TaotaoResult updateDesMessages(String desId, String desMnaview, String desOrgview, String desLeaderview, String desResult,String signatureOne, String dateOne, String signatureTwo, String dateTwo, String signatureThree, String dateThree) {
        TaotaoResult result = new TaotaoResult();
        CmsDestroy cmsDestroy = cmsDestroyMapper.selectByPrimaryKey(Integer.valueOf(desId));
        cmsDestroy.setDesMnaview(desMnaview);
        cmsDestroy.setDesOrgview(desOrgview);
        cmsDestroy.setDesLeaderview(desLeaderview);
        cmsDestroy.setDesModifiedtime(new Date());
        cmsDestroy.setDesExtraone(desResult);
        cmsDestroy.setDesExtratwo(signatureOne);
        cmsDestroy.setDesExtrathree(dateOne);
        cmsDestroy.setDesExtrafour(signatureTwo);
        cmsDestroy.setDesExtrafive(dateTwo);
        cmsDestroy.setDesExtrasix(signatureThree);
        cmsDestroy.setDesExtraseven(dateThree);
        if(desResult.equals("同意")){
            cmsDestroy.setDesStatus(2);
        }else{
            cmsDestroy.setDesStatus(3);
        }
        int i = cmsDestroyMapper.updateByPrimaryKeySelective(cmsDestroy);
        if(i != 1){
            result.setStatus(101);
        }else{
            result.setStatus(200);
        }
        return result;
    }

    @Override
    public TaotaoResult inserDesMessages(String desCode, String desApartment, String desPerson, String desDate, String desArcview, String desPersonview, String desLeaderview, List<DestroyCommonResult> destroyCommonResults, String desResult, String signatureOne, String dateOne, String signatureTwo, String dateTwo, String signatureThree, String dateThree) {
        TaotaoResult result = new TaotaoResult();
        CmsDestroy cmsDestroy = new CmsDestroy();
        cmsDestroy.setDesCode(desCode);
        cmsDestroy.setDesApartment(desApartment);
        cmsDestroy.setDesPerson(desPerson);
        cmsDestroy.setDesDate(desDate);
        cmsDestroy.setDesLeaderview(desLeaderview);
       /* cmsDestroy.setDesOlddate(oldDate);
        cmsDestroy.setDesRemarks(remarks);*/
        cmsDestroy.setDesOrgview(desPersonview);
        cmsDestroy.setDesMnaview(desArcview);
        cmsDestroy.setDesExtraone(desResult);
        cmsDestroy.setDesExtratwo(signatureOne);
        cmsDestroy.setDesExtrathree(dateOne);
        cmsDestroy.setDesExtrafour(signatureTwo);
        cmsDestroy.setDesExtrafive(dateTwo);
        cmsDestroy.setDesExtrasix(signatureThree);
        cmsDestroy.setDesExtraseven(dateThree);
        cmsDestroy.setDesCreatetime(new Date());
        if("".equals(desResult)){
            cmsDestroy.setDesStatus(1);
        }else{
            cmsDestroy.setDesStatus(2);
        }
        int i = cmsDestroyMapperSon.insertCmsDestroy(cmsDestroy);
        if(i!=1){
            result.setStatus(101);
        }else{
            Integer desId = cmsDestroy.getDesId();
            for (DestroyCommonResult destroyCommonResult:destroyCommonResults) {
                Long arcId = destroyCommonResult.getArcId();
                Integer arcSign = destroyCommonResult.getArcSign();
                CmsDestroyArchive cmsDestroyArchive = new CmsDestroyArchive();
                cmsDestroyArchive.setDmDesid(desId);
                cmsDestroyArchive.setDmArcId(arcId.intValue());
                cmsDestroyArchive.setDmArctype(arcSign);
                cmsDestroyArchive.setDmCreateTime(new Date());
                cmsDestroyArchive.setDmExtrathree(destroyCommonResult.getApprOldDate());
                cmsDestroyArchive.setDmExtratwo(destroyCommonResult.getApprRemarks());
                cmsDestroyArchive.setDmExtraone(destroyCommonResult.getApprCode());
                cmsDestroyArchiveMapper.insertSelective(cmsDestroyArchive);
            }
            result.setStatus(200);
            result.setMsg(desId.toString());
        }
        return result;
    }

    @Override
    public TaotaoResult editDesMessages(String desId, String desCode, String desApartment, String desPerson, String desDate, String desArcview, String desPersonview, String desLeaderview, List<DestroyCommonResult> destroyCommonResults, String desResult, String signatureOne, String dateOne, String signatureTwo, String dateTwo, String signatureThree, String dateThree) {
        TaotaoResult result = new TaotaoResult();
        CmsDestroy cmsDestroy = cmsDestroyMapper.selectByPrimaryKey(Integer.valueOf(desId));
        cmsDestroy.setDesCode(desCode);
        cmsDestroy.setDesApartment(desApartment);
        cmsDestroy.setDesPerson(desPerson);
        cmsDestroy.setDesDate(desDate);
        cmsDestroy.setDesLeaderview(desLeaderview);
        /*cmsDestroy.setDesOlddate(oldDate);
        cmsDestroy.setDesRemarks(remarks);*/
        cmsDestroy.setDesOrgview(desPersonview);
        cmsDestroy.setDesMnaview(desArcview);
        cmsDestroy.setDesModifiedtime(new Date());
        cmsDestroy.setDesExtraone(desResult);
        cmsDestroy.setDesExtratwo(signatureOne);
        cmsDestroy.setDesExtrathree(dateOne);
        cmsDestroy.setDesExtrafour(signatureTwo);
        cmsDestroy.setDesExtrafive(dateTwo);
        cmsDestroy.setDesExtrasix(signatureThree);
        cmsDestroy.setDesExtraseven(dateThree);
        if("".equals(desResult)){
            cmsDestroy.setDesStatus(1);
        }else{
            cmsDestroy.setDesStatus(2);
        }
        int i = cmsDestroyMapper.updateByPrimaryKeySelective(cmsDestroy);
        if(i != 1){
            result.setStatus(101);
        }else{
            result.setStatus(200);
        }
        return result;
    }


}
